package com.ag.wsq.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import com.ag.bean.TbSched;
import com.ag.bean.TbSchedId;
import com.ag.bean.TbUser;
import com.ag.common.IBaseService;
import com.ag.wsq.dao.SchedDAO;
import com.ag.wsq.dao.UserDAO;

public class UploadExcleServiceImpl implements IBaseService<TbSched, String> {

	private static final Logger logger = Logger
			.getLogger(UploadExcleServiceImpl.class);
	private SchedDAO schedDAO;
	private UserDAO userDAO;

	public String saveExcel(Workbook personWb) throws Exception {
		String message = null;

		Sheet sheet = personWb.getSheet(0);
		//List list = new ArrayList();
		int total = 0;
		// 因第一行是各列名称，所以从第二行开始取数据
		for (int i = 1; i < sheet.getRows(); i++) {
			TbSched tbSched = new TbSched();
			TbSchedId tbSchedId = new TbSchedId();
			for (int j = 0; j < sheet.getColumns(); j++) {
				Cell cell = sheet.getCell(j, i);
				String content = cell.getContents();
				if (j == 0) {
					if(content==null||content.equals("")){
						return "";
					}
					SimpleDateFormat dateFormat = new SimpleDateFormat(
							"yyyy-MM-dd");
					Date myDate = null;
					try {
						myDate = dateFormat.parse(content);
						logger.info("scTime-------------" + content);
					} catch (Exception e) {
						e.printStackTrace();
					}
					tbSchedId.setScTime(myDate);
				}

				else if (j == 1) {
					if(content==null||content.equals("")){
						return "第"+i+"岗位名不能为空";
					}
					tbSchedId.setDuId(content);
					logger.info("DuId---------------" + content);
				}

				else if (j == 2) {
					if(content==null||content.equals("")){
						return "第"+i+"班次名不能为空";
					}
					tbSchedId.setDsName(content);					
					logger.info("DsName-------------" + content);
				}

				/*else if (j == 3) {
					if(content==null||content.equals("")){
						return "第"+i+"登录名不能为空";
					}
					
					tbSchedId.setUId(content);
					logger.info("UId----------------"+content);
				}*/
				else if(j == 3){
					if(content==null||content.equals("")){
						return "第"+i+"登录名不能为空";
					}
					logger.info("开始导入名字：－－－");
					String UId = null;
					List list = userDAO.findByUName(content);
					if(list.size()>0){
						TbUser user = (TbUser) list.get(0);
						UId = user.getUId();
					}
					logger.info("要导入的用户登录名："+UId);
					tbSchedId.setUId(UId);
				}
				
			}
			
			tbSched.setId(tbSchedId);
			schedDAO.attachDirty(tbSched);
			total++;
			System.out.println("导入第" + total + "个");
			message = "导入成功,共导入" + total + "个人员信息";

		}
		
		personWb.close();

		return message;
	}

	public SchedDAO getSchedDAO() {
		return schedDAO;
	}

	public void setSchedDAO(SchedDAO schedDAO) {
		this.schedDAO = schedDAO;
	}

	public boolean delete(String id) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean delete(TbSched t) {
		// TODO Auto-generated method stub
		return false;
	}

	public TbSched findById(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean save(TbSched t) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean update(TbSched t) {
		// TODO Auto-generated method stub
		return false;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

}
